package com.dianzhihan.cashier.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author Dianzhi Han
 * @version 1.0
 * @description: TODO
 * @date 2023/2/5 19:04
 */
@Slf4j
@ControllerAdvice
//处理全局异常，把运行正常代码和异常代码分开来
public class CashControllerAdvice {
    // 关于错误处理的内容，单独扔到一个地方去
    @ExceptionHandler(ErrorRedirectException.class)
    public String logAndRedirect(ErrorRedirectException exc, RedirectAttributes redirectAttributes) {
        // 暂时所有的错误都重定向到错误页

        log.debug("{}失败: {}",exc.getModule(), exc.getError());

        redirectAttributes.addAttribute("error",exc.getError());
        redirectAttributes.addAttribute("target",exc.getRedirectUrl());
        return "redirect:/error/error.html";
    }
}
